/**
 * Created by ideabobo on 14-6-28.
 */

/***************************************用户模块*******************************************/
var _focuscourseqiandao = null;
$(function(){

    var uinfo = localStorage['userinfo'];
    var ercode = getSearchParam("ercode");
    if(ercode && ercode!=undefined && ercode!='undefined'){
        if(uinfo && $.trim(uinfo)!=""){
            userinfo  = JSON.parse(uinfo);
            toMain();
            setTimeout(function (){
                ajaxCallback("findJ",{id:$.trim(ercode),table:"posts"},function(course){
                    _focuscourseqiandao = course;
                    qiandao();
                });
            },1000);

        }

    }else if(uinfo && $.trim(uinfo)!=""){
        uinfo  = JSON.parse(uinfo);
        $("#lusername").val(uinfo.username);
        $("#lpasswd").val(uinfo.passwd);
        uinfo.remember = "1";
        //if(uinfo.roletype=="2"){
            var tinfo = {};
            tinfo.username = uinfo.username;
            tinfo.passwd = uinfo.passwd;
            tinfo.remember = uinfo.remember;
            login(tinfo);
        //}

    }



});
var userinfo = null;
function login(uinfo){
    var fdata = uinfo || serializeObject($("#loginform"));
    if($.trim(fdata.username)=="" || $.trim(fdata.passwd) == ""){
        showLoader("请输入用户名或密码！",true);
        return;
    }
    fdata.table="user";
    ajaxCallback("findJ",fdata,function(data){
       if(!data){
           showLoader("用户名或密码错误",true);
           changePage("loginpage");
       }else{
           showLoader("登陆成功!",true);

           userinfo = data;
           if(fdata.remember == "1"){
                localStorage["userinfo"] = JSON.stringify(data);
           }else{
               localStorage["userinfo"] = "";
           }
           //startLocationTimmer();
           toMain();
       }
    });
}

function logout(){
    userinfo = null;
    toLogin();
}

function toRegister(){
    changePage("registerpage");
    $("#yuanxi").refreshShowSelectMenu(_xibie,"选择院系","title","title");
}

function toLogin(){
    $($(':input','#loginform').get(1)).val("");
    changePage("loginpage");
}

function register(){

    var fdata = serializeObject($("#registerform"));
    if($.trim(fdata.username) == "" || $.trim(fdata.passwd) == "" || $.trim(fdata.tel) == "" || $.trim(fdata.address) == ""){
        showLoader("请填写完整信息!",true);
        return;
    }
    if(fdata.tel.length<11){
        showLoader("电话号码格式不对!",true);
        return;
    }
    if(fdata.passwd != fdata.passwd2){
        showLoader("两次密码不一致!",true);
        return;
    }
    uplaodImg(function(r){
        fdata.img = r;
        fdata.table="user";
        ajaxCallback("findJ",{username:fdata.username,table:"user"},function(d){
            if(!d){
                ajaxCallback("saveJ",fdata,function(r){
                    if(r){
                        showLoader("注册成功!",true);
                        toLogin();
                    }else{
                        showLoader("注册失败请稍候再试!",true);
                    }
                });
            }else{
                showLoader("用户名已存在!",true);
            }
        });
    });

};

function myinfo(){
    changePage("userinfopage");
    $("#editbutton").hide();
    $("#vusername").text(userinfo.username);
    $("#vtel").val(userinfo.tel);
    $("#vqq").val(userinfo.qq);
    $("#vwechat").val(userinfo.wechat);
    $("#vsex").val(userinfo.sex);
    $("#vbirth").val(userinfo.birth);
    $("#vemail").val(userinfo.email);
    $("#fname1").val(userinfo.fname);
    $("#yuanxi1").val(userinfo.yuanxi);
    $("#zhuanye1").val(userinfo.zhuanye);
    $("#nianji1").val(userinfo.nianji);
    $("#xuehao1").val(userinfo.xuehao);
}

function editUserInfo(){
    $("#editbutton").show();
}

function updateUserInfo(){
    var fdata = serializeObject($("#userform"));
    fdata.id = userinfo.id;
    fdata.table="user";
    ajaxCallback("saveJ",fdata,function(r){
        if(r){
            showLoader("保存成功!",true);
            userinfo.qq = fdata.qq;
            userinfo.tel = fdata.tel;
            userinfo.wechat = fdata.wechat;
            userinfo.email = fdata.email;
            userinfo.birth = fdata.birth;
            userinfo.sex = fdats.sex;
        }else{
            showLoader("保存失败,请稍候再试!",true);
        }
    });
}

function toChangePasswd(){
    $("#pusername").text("用户名:"+userinfo.username);
    changePage("passwdpage");
}

function changePasswd(){
    var fdata = serializeObject($("#passwdform"));
    fdata.id = userinfo.id;
    if(fdata.oldpasswd != userinfo.passwd){
        showLoader("原始密码错误！",true);
        return;
    }
    if($.trim(fdata.passwd) == ""){
        showLoader("密码不能为空！",true);
        return;
    }
    if(fdata.passwd != fdata.passwd2){
        showLoader("两次密码不一致！",true);
        return;
    }
    fdata.table="user";
    ajaxCallback("saveJ",fdata,function(r){
        if(r){
            showLoader("保存成功!",true);
        }else{
            showLoader("保存失败,请稍候再试!",true);
        }
    });
}

function toMyPage(){
    $("#myphoto").attr("src",fileurl+userinfo.img);
    $("#usernamel").text(userinfo.username);
    changePage('mypage');
}

function toForget(){
    changePage("forgetpage");
}

function forgetPasswd(){
    var fdata = serializeObject($("#forgetform"));
    if($.trim(fdata.passwd) == ""){
        showLoader("密码不能为空！",true);
        return;
    }
    if(fdata.passwd != fdata.passwd2){
        showLoader("两次密码不一致！",true);
        return;
    }
    fdata.table="user";
    ajaxCallback("saveJ",fdata,function(data){
        if(data.info==""){
            showLoader("用户不存在",true);
        }else{
            showTipTimer("重置密码成功",function (){
                toLogin();
            });
        }

    });
}

/***************************************用户模块*******************************************/




